/*
 * @Author: LiZn
 * @Date: 2021-08-23 00:11:59
 * @LastEditTime: 2021-08-23 00:26:26
 * @LastEditors: LiZn
 * @Description: file content
 * @FilePath: \todaynews\src\directives\permission.js
 */
import store from '../store'
const permission = {
  inserted(el,binding){
    //el是绑定的dom节点，binding包含有几个属性name,value等,其中name是v-后边的值v-look,name就是look,value是v-look中的值
    const roles = store.getters.roles;
    const {value:rPerson} = binding;
    if(rPerson&&rPerson instanceof Array && rPerson.length>0){
      //判断用户角色中是否有按钮需要的角色
      const hasPermission = rPerson.includes(roles);
      if(!hasPermission){
        el.parentNode && el.parentNode.removeChild(el)
      }
    }else{
      throw new Error(`需要指定按钮要求角色，如v-permisssin="['admin','editor']"`)
    }
  }
}
export default permission